﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dokan;
using System.Threading;
using System.Text.RegularExpressions;

namespace HTML_FS
{
    public partial class Form1 : Form
    {
        DokanOptions opt;
        htmlfs fs;
        Thread DK;
        bool started = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_VisibleChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (Uri.IsWellFormedUriString(URL_textbox.Text, UriKind.Absolute))
            {
                Console.WriteLine("Changing top-level to: " + URL_textbox.Text);
                fs.changeURL(URL_textbox.Text);
                //Console.WriteLine("");
            }
            else
            {
                URL_textbox.Text = "Wrong URI. Please enter right URI.";
            }
            
        }

        public void DokanThread()
        {
            opt=new DokanOptions();
            opt.MountPoint = mountpoint.Text;
            opt.ThreadCount = 5;
            opt.DebugMode = true;
            opt.UseStdErr = true;
            opt.NetworkDrive = true;
            opt.VolumeLabel = "htmlfs";
            fs = new htmlfs("http://habrahabr.ru/");
            int status = DokanNet.DokanMain(opt, fs);
            switch (status)
            {
                case DokanNet.DOKAN_DRIVE_LETTER_ERROR:
                    Console.WriteLine("Drvie letter error");
                    break;
                case DokanNet.DOKAN_DRIVER_INSTALL_ERROR:
                    Console.WriteLine("Driver install error");
                    break;
                case DokanNet.DOKAN_MOUNT_ERROR:
                    Console.WriteLine("Mount error");
                    break;
                case DokanNet.DOKAN_START_ERROR:
                    Console.WriteLine("Start error");
                    break;
                case DokanNet.DOKAN_ERROR:
                    Console.WriteLine("Unknown error");
                    break;
                case DokanNet.DOKAN_SUCCESS:
                    Console.WriteLine("Success");
                    break;
                default:
                    Console.WriteLine("Unknown status: %d", status);
                    break;

            }
        }

        private void mnt_btn_Click(object sender, EventArgs e)
        {
            DK = new Thread(new ThreadStart(DokanThread));
            DK.Start(); //Dokan будет работать в отдельном треде
            started = true;
            unmount_btn.Enabled = true;
            mnt_btn.Enabled = false;
            mountpoint.Enabled = false;
            StatusUpateTimer.Enabled = true;
            Apply_btn.Enabled = true;
            URL_textbox.Enabled = true;
        }

        private void unmount_btn_Click(object sender, EventArgs e)
        {
            if (started)
            {
                DK.Abort();
                opt = null;
                fs = null;
                Console.WriteLine("Unmount: "+DokanNet.DokanRemoveMountPoint(mountpoint.Text).ToString());             
                mnt_btn.Enabled = true;
                unmount_btn.Enabled = false;
                mountpoint.Enabled = true;
                StatusUpateTimer.Enabled = false;
                Apply_btn.Enabled = false;
                URL_textbox.Enabled = false;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            unmount_btn_Click(sender, (EventArgs)e);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void StatusUpateTimer_Tick(object sender, EventArgs e)
        {
            if (fs.status.Length > 1)
            {
                int ind = fs.status.LastIndexOf('\n');
                if (ind >= 0)
                {
                    status_label.Text = fs.status.Substring(ind+1);
                }
            }
            
            //fs.status = "";
        }
    }
}
